package org.write2me.models.presentation
{
	import flash.display.BitmapData;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.utils.ByteArray;
	
	import org.libspark.flartoolkit.core.FLARCode;
	import org.libspark.flartoolkit.core.param.FLARParam;
	import org.libspark.flartoolkit.core.raster.FLARRaster_BitmapData;
	import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
	import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
	import org.write2me.views.KeyboardView;

	public class KeyboardPresentationModel implements IKeyboardPresentationModel
	{
		
		[Embed(source="assets/write2me.pat", mimeType="application/octet-stream")]
		private var pattern:Class;

		[Embed(source="assets/camera_para.dat", mimeType="application/octet-stream")]
		private var params:Class;

		private var fparams:FLARParam;
		
		private var mpattern:FLARCode;
		
		private var vid:Video;
		
		private var cam:Camera;
		
		private var _view:KeyboardView;
		
		private var bmd:BitmapData;
		
		private var raster:FLARRgbRaster_BitmapData;
		
		private var detector:FLARSingleMarkerDetector;
		
		public function KeyboardPresentationModel()
		{
		}
		
		[Autowire(view="true")]
		public function set view(view:KeyboardView):void
		{
			_view = view;
			setupFLAR();
			setupCamera();
			setupBitmap();
		}
		
		private function setupFLAR():void
		{
			fparams = new FLARParam();
			fparams.loadARParam(new params() as ByteArray);
			
			mpattern = new FLARCode(16, 16);
			mpattern.loadARPatt(new pattern());
		}
		
		private function setupCamera():void
		{
			vid = new Video(640, 480);
			cam = Camera.getCamera();
			cam.setMode(640, 480, 30);
			vid.attachCamera(cam);
			
			_view.cameraView.addChild(vid);
		}
		
		private function setupBitmap():void
		{
			bmd = new BitmapData(640, 480);
			bmd.draw(vid);
			
			raster = new FLARRgbRaster_BitmapData(bmd);
			detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);
			
			
		}
	}
}